//
//  LYListController.m
//  WHISPER
//
//  Created by lanou3g on 15/12/20.
//  Copyright © 2015年 梁媛. All rights reserved.
//

#import "LYListController.h"
#import "YPMakeURL.h"
#import "YPNetWorking.h"
#import "LYModel.h"
#import "LYsmallModel.h"
#import "UIImageView+WebCache.h"
#import "LYListCell.h"
#import "LYAuthInfoCell.h"
#import "RadioDetailListModel.h"
#import "RadioInfoModel.h"
#import "UIImageView+WebCache.h"
#import "MJRefresh.h"
#import "YPSongController.h"
#import "YPUser.h"

@interface LYListController ()

@property(nonatomic,strong)NSMutableArray *allListArr;
@property(nonatomic,strong)RadioInfoModel *Rmodel;

@end

@implementation LYListController
- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [leftButton setImage:[UIImage imageNamed:@"iconfont-fanhui"] forState:UIControlStateNormal];
    [leftButton setFrame:CGRectMake(15, 10,30, 30)];
    [leftButton addTarget:self action:@selector(leftAction) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationController.navigationBar addSubview:leftButton];
    [self.tableView registerNib:[UINib nibWithNibName:@"LYListCell" bundle:nil] forCellReuseIdentifier:@"LYListCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"LYAuthInfoCell" bundle:nil] forCellReuseIdentifier:@"LYAuthInfoCell"];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshAction)];
    [self handleData];
}
- (void)refreshAction{
    [self handleData];
}
- (void)handleData{
    YPMakeURL *makeURL = [YPMakeURL shareMakeURL];
    YPNetWorking *netWorking = [YPNetWorking shareNetWorking];
    [makeURL makeRadioDitailListDicWithString:self.passId];
    [netWorking getDictWithURL:makeURL.radioDitailURL dict:makeURL.radioDitailListDic success:^(NSDictionary *dic) {
        [self.allListArr removeAllObjects];
        for (NSDictionary *diction in dic[@"data"][@"list"]) {
            RadioDetailListModel *model = [[RadioDetailListModel alloc] init];
            [model setValuesForKeysWithDictionary:diction];
            model.authorinfo = [[YPUser alloc]init];
            [model.authorinfo setValuesForKeysWithDictionary:diction[@"playInfo"][@"authorinfo"] ];
            [self.allListArr addObject:model];
            self.Rmodel = [[RadioInfoModel alloc] init];
            [_Rmodel setValuesForKeysWithDictionary:dic[@"data"][@"radioInfo"] ];
            _Rmodel.userinfo = [[LYsmallModel alloc] init];
            [_Rmodel.userinfo setValuesForKeysWithDictionary:dic[@"data"][@"radioInfo"][@"userinfo"]];
        }
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    } fail:^(NSError *err) {
        
    }];
}
- (void)headView{
    UIImageView *touView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150)];
    [touView sd_setImageWithURL:[NSURL URLWithString:_Rmodel.coverimg]];
    self.tableView.tableHeaderView = touView;
}

- (void)leftAction{
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.allListArr.count + 1;
    }

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 100;
    }
    return 80;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        LYAuthInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LYAuthInfoCell" forIndexPath:indexPath];
        [cell.imageInfo sd_setImageWithURL:[NSURL URLWithString:_Rmodel.coverimg]];
        cell.imageInfo.layer.masksToBounds = YES;
        cell.imageInfo.layer.cornerRadius = 22;
        cell.introduceLabel.text = _Rmodel.desc;
        cell.authorLabel.text = _Rmodel.userinfo.uname;
        cell.countLabel.text = [NSString stringWithFormat:@"%ld",(long)_Rmodel.musicvisitnum];
        cell.selectionStyle  = UITableViewCellSelectionStyleNone;
        return cell;
    }else{
     LYListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LYListCell" forIndexPath:indexPath];
    RadioDetailListModel *model = self.allListArr[indexPath.row - 1];
        [cell.LeftImageView sd_setImageWithURL:[NSURL URLWithString:model.coverimg]];
        cell.titleLabel.text = model.title;
        cell.countLabel.text = [NSString stringWithFormat:@"%ld",(long)model.musicVisit];
        cell.selectionStyle  = UITableViewCellSelectionStyleNone;
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row != 0) {
        YPSongController *songVC = [YPSongController shareSongController];
        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:songVC];
        RadioDetailListModel *model = self.allListArr[indexPath.row - 1];
        songVC.tingId = model.tingid;//传递当前播放广播的tingID
        songVC.tempListArr = self.allListArr;//把歌曲列表传过去
        songVC.index = indexPath.row - 1;//当前播放歌曲的下标
        [self.navigationController presentViewController:nav animated:YES completion:nil];
    }
}

- (NSMutableArray *)allListArr{
    if (!_allListArr) {
        _allListArr = [NSMutableArray array];
    }
    return _allListArr;
}

@end
